/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 * Description: UDK operations header file
 * Author: -
 * Create: 2021.4.19
 */

#ifndef UDK_OPERATIONS_H
#define UDK_OPERATIONS_H

#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>

#include "udk_ring.h"
#include "udk_memzone.h"
#include "udk_mempool.h"
#include "udk_mbuf.h"

struct udk_melem {
    void *addr;       /* virtual address */
    uint64_t iova;    /* IO address */
    uint64_t page_sz; /* page size of underlying memory */
    int socket_id;    /* NUMA socket ID */
    int rsvd;
};

struct udk_callback_ops {
    /* udk_malloc */
    void (*free)(void *addr);
    void *(*malloc_socket)(const char *type, size_t size, uint32_t align, int32_t socket_arg);

    /* udk_ring */
    struct udk_ring *(*ring_create)(const char *name, uint32_t count, int32_t socket_id, uint32_t flags);
    struct udk_ring *(*ring_lookup)(const char *name);
    void (*ring_free)(struct udk_ring *r);
    int (*ring_dequeue)(struct udk_ring *r, void **obj_p);
    int (*ring_enqueue)(struct udk_ring *r, void *obj);
    int (*ring_full)(const struct udk_ring *r);
    void (*ring_dump)(FILE *f, const struct udk_ring *r);
    uint32_t (*ring_count)(const struct udk_ring *r);
    uint32_t (*ring_free_count)(const struct udk_ring *r);

    /* udk_hash */

    /* udk_memzone */
    int (*memzone_free)(const struct udk_memzone *mz);
    const struct udk_memzone *(*memzone_lookup)(const char *name);
    const struct udk_memzone *(*memzone_reserve)(const char *name, size_t len, int socket_id, uint32_t flags);
    const struct udk_memzone *(*memzone_reserve_aligned)(const char *name, size_t len,
        int socket_id, uint32_t flags, uint32_t align);

    /* udk_mempool */
    int (*mempool_get_bulk)(struct udk_mempool *mp, void **obj_table, uint32_t n);
    void (*mempool_put)(struct udk_mempool *mp, void *obj);
    void (*mempool_free)(struct udk_mempool *mp);
    uint32_t (*mempool_avail_count)(const struct udk_mempool *mp);
    uint32_t (*mempool_in_use_count)(const struct udk_mempool *mp);
    struct udk_mempool *(*mempool_lookup)(const char *name);
    struct udk_mempool *(*mempool_create)(const char *name, uint32_t n, uint32_t elt_size,
        uint32_t cache_size, uint32_t private_data_size, udk_mempool_ctor_t *mp_init, void *mp_init_arg,
        udk_mempool_obj_cb_t *obj_init, void *obj_init_arg, int socket_id, uint32_t flags);

    /* udk_mbuf */
    struct udk_mbuf *(*pktmbuf_alloc)(void *mp);
    int (*pktmbuf_alloc_bulk)(void *mp, struct udk_mbuf **mbufs, uint32_t count);
    void (*pktmbuf_free)(struct udk_mbuf *m);
    struct udk_mempool *(*pktmbuf_pool_create)(const char *name, uint32_t n, uint32_t cache_size,
        uint16_t priv_size, uint16_t data_room_size, int socket_id);

    int (*mheap_alloc)(const char *type, size_t size, int socket_arg, unsigned int flags,
        size_t align, size_t bound, bool contig, struct udk_melem *mem);
    int (*mheap_free)(void *addr);
};

extern struct udk_callback_ops g_udk_reg_ops;

int32_t udk_register_ops(struct udk_callback_ops *reg_ops);

#endif